package Fenetres;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.border.EmptyBorder;

import utils.Repertoire;

public class ConsultDeroulement extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final JPanel contentPanel = new JPanel();
	JComboBox<File> comboBox;
	DefaultComboBoxModel<File> model = new DefaultComboBoxModel<>();
	JTextPane textPane;

	/**
	 * Create the dialog.
	 */
	public ConsultDeroulement() {
		setTitle("Deroulement du sc\u00E9nario ");
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BorderLayout(0, 0));
		{
			textPane = new JTextPane();
			contentPanel.add(textPane, BorderLayout.CENTER);
		}
		{
			loadFiles();
			comboBox = new JComboBox<>(model);
			comboBox.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					LoadFile();
				}
			});
			contentPanel.add(comboBox, BorderLayout.NORTH);
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}

	public void loadFiles() {
		String dir = "files/deroule_txt";
		File file = new File(dir);
		if (file.isDirectory()) {
			File[] files = file.listFiles();
			for (int i = 0; i < files.length; i++) {
				if (files[i].getName().endsWith(".txt")) {
					this.model.addElement(files[i]);
				}
			}
		}
	}

	public void LoadFile() {
		File file = (File) model.getSelectedItem();
		StringBuilder sb = Repertoire.ReadTxtFile(file);
		this.textPane.setText(sb.toString());
	}

}
